from collections import OrderedDict

from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


def meiduo_jwt_response_payload_handler(token, user=None, request=None):
    """重写生成token返回的数据"""
    return {
        'token': token,
        'username': user.username,
        'id': user.id
    }


class PageNum(PageNumberPagination):
    """重写PageNum分页"""
    page_size = 10
    page_size_query_param = 'pagesize'
    max_page_size = 20

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('pages', self.page.paginator.num_pages),
            ('page', self.page.number),
            ('lists', data)
        ]))
